'use strict';

const Controller = require('egg').Controller;
let rules = {
  title: {
    type: 'string',
    required: true,
    desc: '作品标题'
  },
  cover: {
    type: 'string',
    required: true,
    desc: '作品封面'
  },
  category_id: {
    type: 'int',
    required: true,
    desc: '分类id'
  },
  desc: {
    type: 'string',
    required: false,
    desc: '作品描述'
  }
}
class VideoController extends Controller {
  async save() {
    const { ctx } = this
    let user_id = ctx.authUser.id;
    const { title, cover, category_id, desc } = ctx.request.body
    ctx.validate(rules)
    let danmu_count = Math.random() * 999 + 1
    let play_count = Math.random() * 999 + 1
    const video = await ctx.model.Video.create({
      title, cover, category_id, desc, user_id, danmu_count,play_count
    })
    ctx.apiSuccess(video)
  }
  async getVideoInfo() {
    const { ctx } = this
    let id = ctx.params.id
    let res = await ctx.model.Video.findOne({
      where: {
        id
      }
    })
    ctx.apiSuccess(res)

  }
  async index() {
    const { ctx } = this;
    const user_id = ctx.query.user_id;
    ctx.validate({ user_id: { required: true, type: 'int', desc: '用户id' } })
    const res = await ctx.page('Video', { user_id })
    ctx.apiSuccess(res)
  }

  async update() {
    const { ctx } = this;
    const user_id = ctx.authUser.id
    const { title, cover, category_id, desc } = ctx.request.body
    let video = await ctx.model.Video.findOne({
      where: {
        id: ctx.params.id,
        user_id
      }
    })
    if (!video) {
      ctx.throw(404, '该记录不存在')
    }
    const res = await video.update({
      title, cover, category_id, desc
    })
    ctx.apiSuccess(res)
  }

  async delete() {
    const { ctx } = this;
    const user_id = ctx.authUser.id;
    ctx.validate({ id: { type: 'int', required: true, desc: '视频id' } })
    const id = ctx.params.id;
    const video = await ctx.model.Video.findOne({
      where: {
        id,
        user_id
      }
    })
    if (!video) {
      ctx.throw(404, '该作品不存在')
    }
    await video.destroy()
    ctx.apiSuccess('ok')
  }

  async list() {
    let { ctx } = this
    ctx.validate({
      category_id: { type: 'int', required: true, desc: '分类id' },
      page: { type: 'int', desc: '页码' }
    })
    let { category_id } = ctx.params
    const res = await ctx.page('Video', { category_id })
    ctx.apiSuccess(res)
  }

  async comment() {
    const { ctx } = this
    ctx.validate({ id: { type: 'int', required: true, desc: '作品id' } })
    const video_id = ctx.params.id
    // const Op = ctx.Sequelize.Op;
    const res = await ctx.model.Comment.findAll({
      where: {
        video_id,
        reply_id: null
      },
      include: [{
        model: ctx.model.Comment,
        include: [{
          model: ctx.model.User,
          as: "send_user",
          attributes: ['id', 'username', 'nickname']
        }, {
          model: ctx.model.User,
          as: "reply_user",
          attributes: ['id', 'username', 'nickname']
        }]
      }, {
        model: ctx.model.User,
        as: "send_user",
        attributes: ['id', 'username', 'nickname', 'avatar']
      }, {
        model: ctx.model.User,
        as: "reply_user",
        attributes: ['id', 'username', 'nickname', 'avatar']
      }], order: [
        ['id', 'DESC']
      ]

    })
    ctx.apiSuccess(res)
  }

  //热门视频
  async index_data() {
    let { ctx } = this
    ctx.validate({ type: { type: 'int', required: true, desc: '选择类型' } })
    let { type } = ctx.query
    let options={}
    if (type === 1) {
      options.order = [
        ['danmu_count', 'DESC']
      ]
    } else if (type === 2) {
      options.order = [
        ['play_count', 'DESC']
      ]
    }
    let res = await ctx.page('Video', {}, options)
    ctx.apiSuccess(res)
  }

  async video_count(){
    let {ctx}=this
    let res=await ctx.model.Video.count()
    ctx.apiSuccess(res)
  }

  async read() {
    let { ctx } = this
    let Op = this.app.Sequelize.Op;
    ctx.validate({
      id: { type: 'int', required: true, desc: '作品id' },
      user_id: { type: 'int', desc: '用户id' }
    })
    let user_id = null
    if (ctx.query.user_id) {
      user_id = ctx.query.user_id
    }
    let id = ctx.params.id
    let fensCount = 0
    if (user_id !== null) {
      if (!await ctx.model.User.findOne({
        where: {
          id: user_id
        }
      })) {
        return ctx.apiFail('此用户不存在')
      }

    }

    let video = await ctx.model.Video.findOne({
      where: {
        id
      },
      include: [{
        model: ctx.model.VideoDetail
      }, {
        model: ctx.model.User,
        attributes: ['id', 'username', 'nickname', 'avatar']
      }]
    })

    if (video) {
      fensCount = await ctx.service.user.getFenCount(video.user_id)
      console.log(video)
      let { category_id, play_count } = video
      let res = await video.update({
        play_count: play_count + 1
      })
      let recommendList = await ctx.model.Video.findAll({
        where: {
          category_id,
          id: {
            [Op.ne]: id
          }
        },
        offset: 0,
        limit: 6,
        order: [
          ['id', 'DESC']
        ],

      })
      if (user_id) {
        let isFava = !!(await this.app.model.Fava.findOne({
          where: {
            video_id: id,
            user_id
          }
        }))
        if (user_id !== video.user_id) {
          let isFollow = await this.service.user.isFollow(user_id, video.user_id)
          video = JSON.parse(JSON.stringify(video))
          video.user.fensCount = fensCount
          return ctx.apiSuccess({ video, recommendList, isFava, isFollow })
        } else {
          video = JSON.parse(JSON.stringify(video))
          video.user.fensCount = fensCount
          return ctx.apiSuccess({ video, recommendList, isFava })
        }
      }
      video = JSON.parse(JSON.stringify(video))
      video.user.fensCount = fensCount
      console.log(video)
      return ctx.apiSuccess({ video, recommendList })
    }
    ctx.apiFail('此视频不存在')
  }

  async search() {
    let { ctx } = this
    ctx.validate({ keyword: { type: 'string', required: true, desc: '搜索关键字' } })
    let { keyword } = ctx.query
    let Op = this.app.Sequelize.Op
    let res = await ctx.page('Video', {
      title: {
        [Op.like]: '%' + keyword + '%'
      }
    })
    ctx.apiSuccess(res)

  }
}

module.exports = VideoController;
